package com.company.cloud.oss.client;


import cn.hutool.core.codec.Base64Decoder;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.oss.config.OssConfig;
import com.company.cloud.oss.modules.api.req.*;
import com.company.cloud.oss.modules.api.service.OssRemoteService;
import com.company.cloud.oss.modules.base.service.OssStoreInfoService;
import com.company.cloud.oss.modules.cloud.OSSFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;



@RestController
@Service
public class OssRemoteServiceImpl implements OssRemoteService {

    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private OssStoreInfoService ossStoreInfoService;

    @Override
    public BaseClientResult<String> uploadInputStream(UploadInputStreamParam param) {
        try{
            return BaseClientResult.success(OSSFactory.build(ossConfig).upload(param.getInputStream(), param.getPath()));
        } catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> uploadByte(UploadByteParam param) {
        try{
            return BaseClientResult.success(OSSFactory.build(ossConfig).uploadSuffix(param.getData(), param.getSuffix()));
        } catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> uploadBase64(UploadBase64Param param) {
        try{
            UploadByteParam pa = new UploadByteParam();
            pa.setData(Base64Decoder.decode(param.getBase64()));
            pa.setSuffix(param.getSuffix());
            return uploadByte(pa);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> uploadSuffix(UploadSuffixParam param) {
        try {
            return BaseClientResult.success(OSSFactory.build(ossConfig).uploadSuffix(param.getInputStream(), param.getSuffix()));
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

}
